//
//  GameManager.m
//  SpaceViking
//
//  Created by gnt on 1/6/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "GameManager.h"
#import "GameScene.h"
#import "GameScene2.h"
#import "MainMenuScene.h"

@implementation GameManager
static GameManager* _sharedGameManager = nil;
@synthesize hasPlayerDied;

+ (GameManager*)sharedGameManager {
    @synchronized([GameManager class])                
    {
        if(!_sharedGameManager)                           
            [[self alloc] init]; 
        return _sharedGameManager;                        
    }
    return nil; 
}

+ (id)alloc 
{
    @synchronized ([GameManager class])          
    {
        NSAssert(_sharedGameManager == nil,
                 @"Attempted to allocated a second instance of the Game Manager singleton");
        _sharedGameManager = [super alloc];
        return _sharedGameManager; 
    }
    return nil;  
}

- (id)init {                                                        // 8
    self = [super init];
    if (self != nil) {
        // Game Manager initialized
        CCLOG(@"Game Manager Singleton, init");
        hasPlayerDied = NO;
        currentScene = kNoSceneUninitialized;
    }
    return self;
}

- (void)runSceneWithID: (SceneTypes)sceneID {
    SceneTypes oldScene = currentScene;
    currentScene = sceneID;
    id sceneToRun = nil;
    
    switch (sceneID) {
        case kMainMenuScene:
            sceneToRun = [MainMenuScene node];
            break;
            
//        case kOptionsScene:
//            sceneToRun = [OptionsScene node];
//            break;
//            
//        case kCreditsScene:
//            sceneToRun = [CreditsScene node];
//            break;
//        
//        case kIntroScene:
//            sceneToRun = [IntroScene node];
//            break;
//            
//        case kLevelCompleteScene:
//            sceneToRun = [LevelCompleteScene node];
            break;
            
        case kGameLevel1:
            sceneToRun = [GameScene node];
            break;
        case kGameLevel2:
            sceneToRun = [GameScene2 node];
            break;
            
        default:
            CCLOG(@"Unknown ID, cannot switch scenes");
            return;
            break;
    }
    
    if(sceneToRun == nil) {
        currentScene = oldScene;
        return;
    }
    
    if ([[CCDirector sharedDirector] runningScene] == nil) {
        [[CCDirector sharedDirector] runWithScene:sceneToRun];
        
    } else {
        [[CCDirector sharedDirector] replaceScene:sceneToRun];
    }   
}

- (CGSize)getDimensionsOfCurrentScene {
    CGSize screenSize = [[CCDirector sharedDirector]winSize];
    CGSize levelSize;
    switch (currentScene) {
        case kGameLevel1:
            levelSize = screenSize;
            break;
        case kGameLevel2:
            levelSize = CGSizeMake(screenSize.width * 2.0f,screenSize.height);
            break;
        
        default:
            CCLOG(@"Unknown Scene ID, returning default size");
            levelSize = screenSize;
            break;
    }
    return levelSize;
}
@end
